import * as assert from 'assert';
import Clock from '../../src/timer/Clock';

describe('clock', () => {

  describe('#elapsedTime', () => {
    it('should return 0 when not initialized', () => {
      const clock = new Clock();
      assert.equal(0, clock.elapsedTime);
    });

    it('should return 1000 after one second', (done) => {
      const clock = new Clock();
      setTimeout(() => {
        clock.tick();
        if (clock.elapsedTime < 900 || clock.elapsedTime > 1100) { throw new Error('time error'); }
        done();
      }, 1000);
    });
  });

  describe('#deltaTime', () => {
    it('should return 0 when not initialized', () => {
      const clock = new Clock();
      assert.equal(0, clock.deltaTime);
    });

    it('should return delta after delay', (done) => {
      const clock = new Clock();
      assert.equal(0, clock.deltaTime);

      setTimeout(() => {
        clock.tick();
        if (clock.deltaTime < 5 || clock.deltaTime > 15) { throw new Error('time error'); }

        setTimeout(() => {
          clock.tick();
          if (clock.deltaTime < 15 || clock.deltaTime > 25) { throw new Error('time error'); }
          done();
        }, 20);

      }, 10);
    });
  });

  describe('#elapsedTime / auto-start', () => {
    it('should return 0 when not initialized', () => {
      const clock = new Clock(true);
      assert.equal(0, clock.elapsedTime);
    });

    it('should return 1000 after one second', (done) => {
      const clock = new Clock(true);
      setTimeout(() => {
        if (clock.elapsedTime < 900 || clock.elapsedTime > 1100) { throw new Error('time error'); }
        done();
      }, 1000);
    });
  });

});
